package com.bolt.core.view.components;


import com.bolt.common.collection.IteratorUtil;
import com.bolt.common.utils.StrUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


public class Form extends GroupFormItem {


    private static final String OPEN_TEMPLATE = "form";

    private static final String CLOSE_TEMPLATE = "close_form";

    public static final String PAGECONTEXT_FORMVALUES = "bolt_form_values";

    public static final String FORM_ACTION_URL = "formUrl";

    public static final String FORM_UPDATESTATE = "form_updateState";

    private String saveAction;
    private String viewAction;
    private String updateAction;
    private String queryAction;
    private boolean haveSubmitBtn;
    private String onAjaxComplete;


    private String entity;
    private List<ValidatorRule> validatorRoles;


    public Form() {
        super();
        validatorRoles = new ArrayList<>();
        haveSubmitBtn = true;

    }

    @Override
    protected String getDefaultTemplate() {
        return CLOSE_TEMPLATE;
    }

    @Override
    public String getDefaultOpenTemplate() {
        return OPEN_TEMPLATE;
    }

    @Override
    protected void evaluateValue(HttpServletRequest request) {

    }

    public void addFormItems(FormItem item) {
        if (this.getColumn() == 1) {
            item.setSingleColumn(true);
        }
        if ("file".equals(item.getType()) || "multipleFile".equals(item.getType())) {
            this.addParameter("entity", "multipart/form-data");
        }
        if (item instanceof FieldSet) {
            List<FormItem> newRow = new ArrayList<FormItem>();
            newRow.add(item);
            group.add(newRow);
            group.add(new ArrayList<>());
        } else {
            populateGroupItem(item);
        }
    }

    public void newRow() {
        group.add(new ArrayList<>());
    }

    public void appendFormItems(FormItem item) {
        List<FormItem> row = IteratorUtil.getLast(group);
        row.add(item);
    }

    public String getQueryAction() {
        return queryAction;
    }

    public void setQueryAction(String queryAction) {
        this.queryAction = queryAction;
    }


    public String getSaveAction() {
        return saveAction;
    }

    public void setSaveAction(String saveAction) {
        this.saveAction = saveAction;
    }

    public String getViewAction() {
        return viewAction;
    }

    public void setViewAction(String viewAction) {
        this.viewAction = viewAction;
    }

    public String getEntity() {
        return entity;
    }

    public void setEntity(String entity) {
        this.entity = entity;
    }

    public void addValidators(ValidatorRule v) {
        this.validatorRoles.add(v);
    }

    public List<ValidatorRule> getValidatorRoles() {
        return this.validatorRoles;
    }

    public ValidatorRule findValidator(String id) {
        if (StrUtil.isEmpty(id)) {
            return null;
        }
        ValidatorRule res = null;
        for (ValidatorRule v : validatorRoles) {
            if (v.getId().equals(id)) {
                res = v;
                break;
            }
        }
        return res;
    }

    public String getUpdateAction() {
        if (StrUtil.isEmpty(updateAction)) {
            updateAction = this.saveAction;
        }
        return updateAction;
    }

    public void setUpdateAction(String updateAction) {
        this.updateAction = updateAction;
    }

    public boolean isHaveSubmitBtn() {
        return haveSubmitBtn;
    }

    public void setHaveSubmitBtn(boolean haveSubmitBtn) {
        this.haveSubmitBtn = haveSubmitBtn;
    }


    public String getOnAjaxComplete() {
        return onAjaxComplete;
    }

    public void setOnAjaxComplete(String onAjaxComplete) {
        this.onAjaxComplete = onAjaxComplete;
    }


}
